package csm.web.core.search.mashup.aladin;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import csm.common.StringUtil;
import csm.common.net.HttpRequester;
import csm.web.core.RequestHelper;
import csm.web.core.search.ISearchCommand;
import csm.web.core.search.MashupItem;
import csm.web.core.search.MashupItemList;
import csm.web.core.search.SearchResultInfo;
import csm.web.core.search.mashup.naver.NaverSearchResult;

public class AladinSearchCommand implements ISearchCommand{

	@Override
	public SearchResultInfo doSearch(RequestHelper req) {
		String key = "";
		String uri = "";
		String target = "";
		String query = ""; //검색어

		String keyword ="";
		String searchField = "";

		String start = "1";    // 페이지
		String pageSize = "10"; //pagesize
		String apiUrl ="";
		String targetEncoding = "UTF-8";

		String mashupId = req.getParameter("mashup_id");
		MashupItem itm = MashupItemList.getInstance().getItem(mashupId);

		key = itm.getApiKey();
		uri = itm.getApiBaseUri();
		apiUrl = itm.getApiUrl(); // 더보기 했을때 이동할 사이트 url
		target = itm.getDefaultParam();
		targetEncoding = itm.getRequestEncoding();

		if(StringUtil.isNullOrEmpty(target))
			return new  NaverSearchResult();

		if(StringUtil.isNullOrEmpty(uri))
			return new AladinSearchResult();

		if( StringUtil.isNullOrEmpty(targetEncoding))
			targetEncoding = "";

		keyword = req.getParameter("keyword");
		if( StringUtil.isNullOrEmpty(keyword))
			return new  AladinSearchResult();

		String[] keywords = StringUtil.split(keyword, ":");

		if(keywords == null )
			return new  AladinSearchResult();

		try {
			if (keywords.length == 1) {
				searchField = "";
				query = keyword;
				if( !StringUtil.isNullOrEmpty(targetEncoding))
					query = URLEncoder.encode(query, targetEncoding);

			} else {
				searchField = StringUtil.split(keyword, ":")[0];
				query = StringUtil.split(keyword, ":")[1];
				if (!StringUtil.isNullOrEmpty(targetEncoding))
					query = URLEncoder.encode(query, targetEncoding);
			}
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
			if (keywords.length == 1) {
				query = keyword;
			} else {
				searchField = StringUtil.split(keyword, ":")[0];
				query = StringUtil.split(keyword, ":")[1];
			}
		}

		HttpRequester hq = new HttpRequester();
		hq.setConnectTimeout(10000);
		hq.setReadTimeout(20000);
		hq.appendParameter("ttbkey", key);
		hq.appendParameter("Query", query);
		hq.appendParameter("QueryType", "Keyword");
		hq.appendParameter("SearchTarget", target);
		hq.appendParameter("MaxResults", "10");
		hq.appendParameter("start", "1");
		hq.appendParameter("output", "OS");
		hq.appendParameter("Version", "20070901");

		String cmd = hq.getParameterQuery();
		SearchResultInfo result = new  AladinSearchResult();
		try {
			System.out.println("Aladin Mashup Search CMD:" + uri + "?" + cmd);
			String resultXml = hq.executeString(uri + "?" + cmd);

			//System.out.println(resultXml);
			//System.out.println(apiUrl);

			result.parseResult(resultXml);
			result.setEngineQuery(cmd);
			if( !StringUtil.isNullOrEmpty(apiUrl))
				result.setWebPageUrl(StringUtil.replace(apiUrl, "[SEARCH_WORD]", keyword));

			result.setPage(Integer.parseInt(start));
			result.setPageSize(Integer.parseInt(pageSize));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

}
